﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Interop;
using WCintiqHID;
using System.Drawing.Imaging;

namespace TTIServer.TTISettings
{
	/// <summary>
	/// Interaction logic for DisplaySettings.xaml
	/// </summary>
	public partial class DisplaySettings : Window
	{

          Cintiq_K myWC21ux;
		Cintiq_K myWC24hd;
          ScreenFactorClass theSFC;

          double CurrentScreenHeight = SystemParameters.PrimaryScreenHeight;
          double CurrentScreenWidth = SystemParameters.PrimaryScreenWidth;

		  double CintiqHeight;
		  double CintiqWidth;

          System.Windows.Shapes.Rectangle PASRectangle;

          public delegate void PASRectangle_MouseDownHandler(object sender, MouseEventArgs e);

          private static BitmapSource CopyPrimaryScreen()
          {
               using (var screenBmp = new Bitmap(
                   (int)SystemParameters.PrimaryScreenWidth,
                   (int)SystemParameters.PrimaryScreenHeight,
                   System.Drawing.Imaging.PixelFormat.Format32bppArgb))
               {
                    using (var bmpGraphics = Graphics.FromImage(screenBmp))
                    {
                         bmpGraphics.CopyFromScreen(0, 0, 0, 0, screenBmp.Size);
                         return Imaging.CreateBitmapSourceFromHBitmap(
                             screenBmp.GetHbitmap(),
                             IntPtr.Zero,
                             Int32Rect.Empty,
                             BitmapSizeOptions.FromEmptyOptions());
                    }
               }
          }  


		public DisplaySettings( Cintiq_K wCintiqRef, ScreenFactorClass mySF )
		{
			this.InitializeComponent();


               myWC21ux = wCintiqRef;

               DisplayImg.BeginInit();
               DisplayImg.Source = CopyPrimaryScreen();
               DisplayImg.EndInit();
               //TODO: Get the Values from Whoever!
			   theSFC = mySF;
			// Insert code required on object creation below this point.
               GetFactorValues();
		}

          public void GetFactorValues()
          {

               
               txtOffsetX.Text = theSFC.CintiqXLoc.ToString();
               txtOffsetY.Text = theSFC.CintiqYLoc.ToString();
			   txtPixelsX.Text = SystemParameters.PrimaryScreenWidth.ToString();
			   txtPixelsY.Text = SystemParameters.PrimaryScreenHeight.ToString();
               sfEnable.IsChecked = theSFC.Enable;

               PASRectangle = new System.Windows.Shapes.Rectangle();
			   switch (myWC21ux.GetPID())
			   {
				   case 0xCC:
					   CintiqHeight = 1200;
					   CintiqWidth = 1600;
						PASRectangle.Width = 600;
						PASRectangle.Height = 450;
					   break;
				   case 0xF8:
					   CintiqHeight = 1200;
					   CintiqWidth = 1920;
						PASRectangle.Width = 720;
						PASRectangle.Height = 450;
					   break;
			   }

               PASRectangle.StrokeThickness = 1;
               PASRectangle.Opacity = 0.85;
               PASRectangle.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0xFF, 0, 0));
               PASRectangle.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(0xff, 0x97, 0x97, 0x97));
               PASRectangle.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
               PASRectangle.RenderTransform = new TranslateTransform(theSFC.CintiqXLoc, theSFC.CintiqYLoc);
               PASRectangle.Cursor = Cursors.SizeAll;
               
               PASRectangle.MouseLeftButtonDown += new MouseButtonEventHandler(PASRectangle_MouseLeftButtonDown);
               PASRectangle.MouseMove += new MouseEventHandler(PASRectangle_MouseMove);
               PASRectangle.MouseLeftButtonUp += new MouseButtonEventHandler(PASRectangle_MouseLeftButtonUp);

               DisplayCanvas.Children.Add(PASRectangle);


          }

          private void PASRectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {





          }

          private void PASRectangle_MouseMove(object sender, MouseEventArgs e)
          {



          }

          private void PASRectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
          {



          }

          private void RadioButton_Checked(object sender, RoutedEventArgs e)
          {

          }

          private void Button_Click(object sender, RoutedEventArgs e)
          {

               

               theSFC.CintiqXLoc = UInt16.Parse(txtOffsetX.Text);
               theSFC.CintiqYLoc = UInt16.Parse(txtOffsetY.Text);
			   theSFC.YFactor = (UInt16)((SystemParameters.PrimaryScreenHeight * 1000) / CintiqHeight);
			   theSFC.XFactor = (UInt16)((SystemParameters.PrimaryScreenWidth * 1000) / CintiqWidth); 
               theSFC.Enable = (bool)sfEnable.IsChecked;

               myWC21ux.SendNewDisplayCoords(theSFC);
               this.Hide();
               
               
          }
	}
}